package com.zzptc.zzsgz.bdsjws.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.SelectContactFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllContactFragment;
import com.zzptc.zzsgz.bdsjws.utils.MyUtils;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class InsertLianXiRenActivity extends BaseActivity {
    ShowAllContactFragment showAllContactFragment;//第一个碎片
    SelectContactFragment selectContactFragment;
    @BindView(R.id.et_contact)
    EditText etContact;
    @BindView(R.id.Img_add)
    ImageView ImgAdd;
    boolean flag = true;
    List<Contact> list;
    List<Contact> selectlist = new ArrayList<Contact>();
    @BindView(R.id.tv_count)
    TextView tvcount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_insert_lian_xi_ren;
    }

    @Override
    public void initData() {
        tvcount.setText("("+ MyApplication.getSosList().size()+")");
        list = MyUtils.getAllContacts();//获得所有联系人
        if (showAllContactFragment == null) {
            showAllContactFragment = new ShowAllContactFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_show_contact, showAllContactFragment).addToBackStack("contact").commit();
    }

    //处理回退异常
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    @Override
    public void intView() {
        //监听
        etContact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (flag) {
                    if (selectContactFragment == null) {
                        selectContactFragment = new SelectContactFragment();
                    }
                    getFragmentManager().beginTransaction().add(R.id.fl_show_contact, selectContactFragment)
                            .addToBackStack("cortact").commit();
                    flag = false;
                }
            }

            @Override
            public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
                final CharSequence c = charSequence.toString().replace(" ", "").replace("-", "");
                Toast.makeText(InsertLianXiRenActivity.this, charSequence, Toast.LENGTH_SHORT).show();
                selectlist.clear();
                //项 适配器 都可以共用
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int j = 0; j < list.size(); j++) {
                            Contact contact = list.get(j);
                            if (contact.getName().contains(c) || contact.getPhonenum().toString().
                                    replace(" ", "").replace("-", "")
                                    .contains(c)) {
                                selectlist.add(contact);
                            }
                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectContactFragment.getSelectContactList(selectlist);
                            }
                        });
                    }
                }).start();

            }

            @Override
            public void afterTextChanged(Editable editable) {
//当删除完毕  要不就回退到前页  要不显示所有联系人
                if (etContact.getText().length() == 0) {
                    if (getFragmentManager().getBackStackEntryCount() > 1) {
                        getFragmentManager().popBackStack();
                    }
                    flag = true;//再次打开开关
                }
            }
        });

    }

    @OnClick(R.id.Img_add)
    public void onViewClicked() {
        startActivity(new Intent(this, OneKeySOSActivity.class));
    }
}
